package
{
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.Capabilities;
	import flash.system.Security;
	import flash.system.System;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	
	import rpg.Utils.ByteArrayUtil;
	import rpg.Utils.CharVal;
	import rpg.Utils.FPS;
	import rpg.Utils.GameUtils;
	import rpg.Utils.Print;
	import rpg.Utils.TimeControl;
	import rpg.Utils.Val;
	import rpg.animation.AvatarDescription;
	import rpg.animation.Frame;
	import rpg.factory.GameFactory;
	import rpg.manager.AnimationManager;
	import rpg.manager.DataManager;
	import rpg.manager.GameManager;
	import rpg.manager.SceneManager;
	import rpg.manager.UIManager;
	import rpg.resource.*;
	import rpg.role.Hero;
	import rpg.role.SceneObject;
	
	import ui.SkinManager;
	import ui.controls.JButton;
	import ui.controls.JCheckBox;
	import ui.controls.JComboBox;
	import ui.controls.JTextInput;
	import ui.event.EventType;
	import ui.skin.LookAndFeel;

	//[SWF(width='1000', height='600', frameRate=60, backgroundColor=0xE2E2E2)]
	public class Main extends Sprite
	{
		public function Main()
		{
			super(); 
			init(); 
		}
		private var preLoad : PreLoad;

		private function init() : void
		{
			//Security.loadPolicyFile("http://192.168.0.226/crossdomain.xml");
			SceneManager.getInstance().init(stage);
			SceneManager.getInstance().resizeWindows(CharVal.AUTOSWF);
			TimeControl.getInstance().start();
			stage.addChild(new FPS());
			preLoad = new PreLoad();
			preLoad.loadConfig("../../rpg_res/data/config.xml?v=" + Math.random() , onLoadComplete);
		}


		private function onLoadComplete(preLoad : PreLoad) : void
		{
			preLoad.load("preload" , onLoadComplete1);
		}

		private function onLoadComplete1(res : Resources) : void
		{    
			AnimationManager.getInstance().parseEffectDescription(res.getXml("effect"));
  
			var config : ByteArray = res.getByteArray("client.config");
			DataManager.getInstance().parseConfig(config);

			var gm : GameManager = GameManager.getInstance();
			gm.createSelectAnimations();
			gm.createClickAnimation();
			var modelClass : Class = res.getClass("ModelData");
			gm.modelBmd = new modelClass(0 , 0);

			var shortcutUi : Class = res.getClass('ShortcutUi');
			UIManager.shortcutUi = shortcutUi;

			var hpBarFront : Class = res.getClass('HpBarFront');
			var hpBarBackGround : Class = res.getClass('HpBarBackGround');
			var smallShadowClass : Class = res.getClass("ShadowMid");
			gm.hpBarImg = new hpBarFront(0 , 0);
			gm.hpBarBgImg = new hpBarBackGround(0 , 0);
			gm.smallShadow = new smallShadowClass(0 , 0);

			SceneManager.getInstance().mapId = 17;

			UIManager.initClass(res);

			var hero : Hero = GameFactory.getInstance().createRole("角色1" , true) as Hero;
			gm.setHero(hero);
//
			for(var i : int = 0 ; i < 100 ; i++)
			{
				var heroName : String = "角色" + int(Math.random() * 4 + 1);
				var obj : Hero = GameFactory.getInstance().createRole(heroName) as Hero;
				obj.mapX = GameUtils.getIntPixelX(GameUtils.viewX + int(Math.random() * GameUtils.stageWidth));
				obj.mapY = GameUtils.getIntPixelY(GameUtils.viewY + int(Math.random() * GameUtils.stageHeight));
				obj.data.teamID = Val.TEAM_ENEMY;
				obj.name = heroName;
			}

			UIManager.initView();

//			SkinManager.defaultUI = new LookAndFeel();
//			var comboBox : JComboBox = new JComboBox(Val.actionStates);
//			stage.addChild(comboBox);
//			comboBox.x = 70;
//			comboBox.addEventListener(EventType.CHANGE , onChangeEvent);
//
//			rowInput = new JTextInput();
//			colInput = new JTextInput();
//			rowInput.setSize(50 , 20);
//			colInput.setSize(50 , 20);
//			stage.addChild(rowInput);
//			stage.addChild(colInput);
//			rowInput.y = 50;
//			colInput.y = 50;
//			colInput.x = 10;
//			rowInput.x = colInput.x + colInput.width + 5;
//
//			flyBtn = new JButton("飞");
//			flyBtn.move(10 , colInput.y + colInput.height + 5);
//			stage.addChild(flyBtn);
//			flyBtn.addEventListener(MouseEvent.CLICK , onClick);
		}
		private var rowInput : JTextInput;
		private var colInput : JTextInput;
		private var flyBtn : JButton;

		private function onChangeEvent(evt : Event) : void
		{
			var comboBox : JComboBox = evt.currentTarget as JComboBox;
			var state : int = Val.actionStates.indexOf(comboBox.label);
			//GameManager.getInstance().getHero().state = state;
		}

		private function onClick(evt : MouseEvent) : void
		{
			GameManager.getInstance().flyTo(int(colInput.text) , int(rowInput.text));
		}

	}
}